package org.yelongframework.model.support.mybatis.service;

import org.yelongframework.model.service.configuration.ModelServiceConfiguration;
import org.yelongframework.model.support.mybatis.mapper.GenericMapperParamBuilder;
import org.yelongframework.model.support.mybatis.mapping.result.DefaultResultMapBuilder;
import org.yelongframework.model.support.mybatis.mapping.result.DefaultResultMappingBuilder;
import org.yelongframework.model.support.mybatis.mapping.result.ResultMapBuilder;
import org.yelongframework.model.support.mybatis.mapping.result.ResultMappingBuilder;
import org.yelongframework.model.support.mybatis.mapping.statement.DefaultMappedStatementBuilder;
import org.yelongframework.model.support.mybatis.mapping.statement.MappedStatementBuilder;
import org.yelongframework.model.support.mybatis.sql.executor.MybatisSqlExecutor;
import org.yelongframework.sql.executor.SqlExecutor;

/**
 * mybatis模型业务配置
 * 
 * @since 1.0.0
 */
public class MybatisModelServiceConfiguration extends ModelServiceConfiguration {

	/** 映射声明构建器 */
	protected MappedStatementBuilder mappedStatementBuilder;

	// ==================================================sqlExecutor==================================================

	@Override
	public void setSqlExecutor(SqlExecutor sqlExecutor) {
		if (sqlExecutor instanceof MybatisSqlExecutor) {
			setMybatisSqlExecutor((MybatisSqlExecutor) sqlExecutor);
			return;
		}
		throw new UnsupportedOperationException("仅支持MybatisSqlExecutor");
	}

	public void setMybatisSqlExecutor(MybatisSqlExecutor mybatisSqlExecutor) {
		super.setSqlExecutor(mybatisSqlExecutor);
	}

	public MybatisSqlExecutor getMybatisSqlExecutor() {
		return (MybatisSqlExecutor) getSqlExecutor();
	}

	// ==================================================get/set==================================================

	/**
	 * @return 默认的映射声明构建器
	 */
	public MappedStatementBuilder defaultMappedStatementBuilder() {
		ResultMappingBuilder resultMappingBuilder = new DefaultResultMappingBuilder();
		ResultMapBuilder resultMapBuilder = new DefaultResultMapBuilder(resultMappingBuilder);
		return new DefaultMappedStatementBuilder(resultMapBuilder);
	}

	public MappedStatementBuilder getMappedStatementBuilder() {
		return mappedStatementBuilder;
	}

	public void setMappedStatementBuilder(MappedStatementBuilder mappedStatementBuilder) {
		this.mappedStatementBuilder = mappedStatementBuilder;
	}

	public GenericMapperParamBuilder getGenericMapperParamBuilder() {
		return getMybatisSqlExecutor().getGenericMapperParamBuilder();
	}

}
